﻿/*--------------------------------------------------------------------
 * Author : Tharinduni Udugamasuriya
 * Date : 29 October 2014
 * TransactionToolBar : TransactionToolBar User Control's Code Behind
 --------------------------------------------------------------------*/

using CrystalDecisions.CrystalReports.Engine;
using DSPL.Common.Classes;
using DSPL.Common.ViewModel;
using DSPL.Contracts;
using DSPL.Contracts.Common;
using DSPL.Contracts.Interfaces;
using DSPL.Reports.Classes;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Linq;


namespace DSPL.Common
{
    /// <summary>
    /// Interaction logic for TransactionToolBar.xaml
    /// </summary>
    public partial class TransactionToolBar : UserControl
    {
        public TransactionToolBar()
        {
            InitializeComponent();
            if (DesignerProperties.GetIsInDesignMode(this))
                return;

            ResolveDependancies();
        }

        public void ResolveDependancies()
        {
            IUnityContainer container = GlobalVariables.GlobalUnityContainer;
            if (container != null)
            {
                TransactionReadService = container.Resolve<ITransactionReadService>();
                TransactionWriteService = container.Resolve<ITransactionWriteService>();
                ColumnWithValue = container.Resolve<IColumnWithValue>();
                CommonValidations = container.Resolve<ICommonValidations>();
                MessageService = container.Resolve<IMessageService>();
            }
        }

        #region Declaring User Control Internal/Private/Protected Variables

        private string firstFocusElement;
        private int currentIndex;
        DataRow NewDataRow;
        DataRow OldDataRow;
        string oldFilePath;
        private string uniqueKey;
        private bool isSaveEnable;
        private bool isNavigationEnable;
        private int primaryKey;
        private string referenceNo;
        private string primaryKeyField = string.Empty;
        private string referenceNoField = string.Empty;
        private string filterString = string.Empty;
        private string sortString = string.Empty;
        private string title = string.Empty;
        private string reportName = string.Empty;

        #endregion

        #region Declaring Dependency Properties

        #region Visibility of Buttons

        public static readonly DependencyProperty NewVisibilityProperty = DependencyProperty.Register("NewVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty EditVisibilityProperty = DependencyProperty.Register("EditVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty DeleteVisibilityProperty = DependencyProperty.Register("DeleteVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty UndoVisibilityProperty = DependencyProperty.Register("UndoVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty FirstVisibilityProperty = DependencyProperty.Register("FirstVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty PreviousVisibilityProperty = DependencyProperty.Register("PreviousVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty NextVisibilityProperty = DependencyProperty.Register("NextVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty LastVisibilityProperty = DependencyProperty.Register("LastVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty PrintVisibilityProperty = DependencyProperty.Register("PrintVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty ListVisibilityProperty = DependencyProperty.Register("ListVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty SearchVisibilityProperty = DependencyProperty.Register("SearchVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty FilterVisibilityProperty = DependencyProperty.Register("FilterVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty SortVisibilityProperty = DependencyProperty.Register("SortVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty SaveVisibilityProperty = DependencyProperty.Register("SaveVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty CloseVisibilityProperty = DependencyProperty.Register("CloseVisibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty Button1VisibilityProperty = DependencyProperty.Register("Button1Visibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty Button2VisibilityProperty = DependencyProperty.Register("Button2Visibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty Button3VisibilityProperty = DependencyProperty.Register("Button3Visibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty Button4VisibilityProperty = DependencyProperty.Register("Button4Visibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));
        public static readonly DependencyProperty Button5VisibilityProperty = DependencyProperty.Register("Button5Visibility", typeof(Visibility), typeof(TransactionToolBar), new PropertyMetadata(Visibility.Visible));

        #endregion

        public static readonly DependencyProperty ChildTableNameProperty = DependencyProperty.Register("ChildTableName", typeof(string), typeof(TransactionToolBar), new PropertyMetadata("abc"), new ValidateValueCallback(TableNameValidateCallBack));
        public static readonly DependencyProperty ParentTableNameProperty = DependencyProperty.Register("ParentTableName", typeof(string), typeof(TransactionToolBar), new PropertyMetadata("abc"), new ValidateValueCallback(TableNameValidateCallBack));
        public static readonly DependencyProperty LogChildTableProperty = DependencyProperty.Register("LogChildTable", typeof(string), typeof(TransactionToolBar), new PropertyMetadata("abc"), new ValidateValueCallback(TableNameValidateCallBack));
        public static readonly DependencyProperty LogParentTableProperty = DependencyProperty.Register("LogParentTable", typeof(string), typeof(TransactionToolBar), new PropertyMetadata("abc"), new ValidateValueCallback(TableNameValidateCallBack));
        public static readonly DependencyProperty TempChildTableProperty = DependencyProperty.Register("TempChildTable", typeof(string), typeof(TransactionToolBar), new PropertyMetadata("abc"));
        public static readonly DependencyProperty TempParentTableProperty = DependencyProperty.Register("TempParentTable", typeof(string), typeof(TransactionToolBar), new PropertyMetadata("abc"));
        public static readonly DependencyProperty LogTableNameProperty = DependencyProperty.Register("LogTableName", typeof(string), typeof(TransactionToolBar), new PropertyMetadata("abc"), new ValidateValueCallback(TableNameValidateCallBack));
        public static readonly DependencyProperty CurrentRowProperty = DependencyProperty.Register("CurrentRow", typeof(DataRow), typeof(TransactionToolBar), new PropertyMetadata(null, OnCurrentRowChanged));
        public static readonly DependencyProperty SqlTranProperty = DependencyProperty.Register("SqlTran", typeof(SqlTransaction), typeof(TransactionToolBar), new PropertyMetadata(null));
        public static readonly DependencyProperty IsValidProperty = DependencyProperty.Register("IsValid", typeof(bool), typeof(TransactionToolBar), new PropertyMetadata(true));
        public static readonly DependencyProperty FormModeProperty = DependencyProperty.Register("FormMode", typeof(Modes), typeof(TransactionToolBar), new PropertyMetadata(Modes.Undo, FormModeChangedCallBack));
        public static readonly DependencyProperty IgnoreTableListProperty = DependencyProperty.Register("IgnoreTableList", typeof(string), typeof(TransactionToolBar), new PropertyMetadata(""));
        public static readonly DependencyProperty ReportFolderProperty = DependencyProperty.Register("ReportFolder", typeof(string), typeof(TransactionToolBar), new PropertyMetadata(""));
        public static readonly DependencyProperty DtParentTxnDataProperty = DependencyProperty.Register("DtParentTxnData", typeof(DataTable), typeof(TransactionToolBar), new PropertyMetadata(null));
        public static readonly DependencyProperty DtChildTxnDataProperty = DependencyProperty.Register("DtChildTxnData", typeof(DataTable), typeof(TransactionToolBar), new PropertyMetadata(null));
        
        #region Methods Execute Type

        public static readonly DependencyProperty IsDataValidateExecuteProperty = DependencyProperty.Register("IsDataValidateExecute", typeof(bool), typeof(TransactionToolBar), new PropertyMetadata(true));
        public static readonly DependencyProperty IsDataUpdValidateExecuteProperty = DependencyProperty.Register("IsDatUpdaValidateExecute", typeof(bool), typeof(TransactionToolBar), new PropertyMetadata(true));
        public static readonly DependencyProperty IsDataDelValidateExecuteProperty = DependencyProperty.Register("IsDataDelValidateExecute", typeof(bool), typeof(TransactionToolBar), new PropertyMetadata(true));
        public static readonly DependencyProperty IsDataSaveProperty = DependencyProperty.Register("IsDataSave", typeof(bool), typeof(TransactionToolBar), new PropertyMetadata(true));
        public static readonly DependencyProperty IsDataUpdateProperty = DependencyProperty.Register("IsDataUpdate", typeof(bool), typeof(TransactionToolBar), new PropertyMetadata(true));
        public static readonly DependencyProperty IsDataDeleteProperty = DependencyProperty.Register("IsDataDelete", typeof(bool), typeof(TransactionToolBar), new PropertyMetadata(true));
        public static readonly DependencyProperty IsApprovalProperty = DependencyProperty.Register("IsApproval", typeof(bool), typeof(TransactionToolBar), new PropertyMetadata(false));
        public static readonly DependencyProperty IsResetDefaultExecuteProperty = DependencyProperty.Register("IsResetDefaultExecute", typeof(bool), typeof(TransactionToolBar), new PropertyMetadata(true));

        #endregion

        #endregion

        #region Declaring Properties

        public ITransactionReadService TransactionReadService { get; set; }
        public ITransactionWriteService TransactionWriteService { get; set; }
        public IColumnWithValue ColumnWithValue { get; set; }
        public ICommonValidations CommonValidations { get; set; }
        public IMessageService MessageService { get; set; }

        #region Visibility of Buttons

        public Visibility NewVisibility
        {
            get { return (Visibility)GetValue(NewVisibilityProperty); }
            set { SetValue(NewVisibilityProperty, value); }
        }

        public Visibility EditVisibility
        {
            get { return (Visibility)GetValue(EditVisibilityProperty); }
            set { SetValue(EditVisibilityProperty, value); }
        }

        public Visibility DeleteVisibility
        {
            get { return (Visibility)GetValue(DeleteVisibilityProperty); }
            set { SetValue(DeleteVisibilityProperty, value); }
        }

        public Visibility UndoVisibility
        {
            get { return (Visibility)GetValue(UndoVisibilityProperty); }
            set { SetValue(UndoVisibilityProperty, value); }
        }

        public Visibility FirstVisibility
        {
            get { return (Visibility)GetValue(FirstVisibilityProperty); }
            set { SetValue(FirstVisibilityProperty, value); }
        }

        public Visibility PreviousVisibility
        {
            get { return (Visibility)GetValue(PreviousVisibilityProperty); }
            set { SetValue(PreviousVisibilityProperty, value); }
        }

        public Visibility NextVisibility
        {
            get { return (Visibility)GetValue(NextVisibilityProperty); }
            set { SetValue(NextVisibilityProperty, value); }
        }

        public Visibility LastVisibility
        {
            get { return (Visibility)GetValue(LastVisibilityProperty); }
            set { SetValue(LastVisibilityProperty, value); }
        }

        public Visibility PrintVisibility
        {
            get { return (Visibility)GetValue(PrintVisibilityProperty); }
            set { SetValue(PrintVisibilityProperty, value); }
        }

        public Visibility ListVisibility
        {
            get { return (Visibility)GetValue(ListVisibilityProperty); }
            set { SetValue(ListVisibilityProperty, value); }
        }

        public Visibility SearchVisibility
        {
            get { return (Visibility)GetValue(SearchVisibilityProperty); }
            set { SetValue(SearchVisibilityProperty, value); }
        }

        public Visibility FilterVisibility
        {
            get { return (Visibility)GetValue(FilterVisibilityProperty); }
            set { SetValue(FilterVisibilityProperty, value); }
        }

        public Visibility SortVisibility
        {
            get { return (Visibility)GetValue(SortVisibilityProperty); }
            set { SetValue(SortVisibilityProperty, value); }
        }

        public Visibility SaveVisibility
        {
            get { return (Visibility)GetValue(SaveVisibilityProperty); }
            set { SetValue(SaveVisibilityProperty, value); }
        }

        public Visibility CloseVisibility
        {
            get { return (Visibility)GetValue(CloseVisibilityProperty); }
            set { SetValue(CloseVisibilityProperty, value); }
        }

        public Visibility Button1Visibility
        {
            get { return (Visibility)GetValue(Button1VisibilityProperty); }
            set { SetValue(Button1VisibilityProperty, value); }
        }

        public Visibility Button2Visibility
        {
            get { return (Visibility)GetValue(Button2VisibilityProperty); }
            set { SetValue(Button2VisibilityProperty, value); }
        }

        public Visibility Button3Visibility
        {
            get { return (Visibility)GetValue(Button3VisibilityProperty); }
            set { SetValue(Button3VisibilityProperty, value); }
        }

        public Visibility Button4Visibility
        {
            get { return (Visibility)GetValue(Button4VisibilityProperty); }
            set { SetValue(Button4VisibilityProperty, value); }
        }

        public Visibility Button5Visibility
        {
            get { return (Visibility)GetValue(Button5VisibilityProperty); }
            set { SetValue(Button5VisibilityProperty, value); }
        }

        #endregion

        #region General Properties

        public DataTable DtAllMasterDataOriginal { get; private set; }

        public DataTable DtAllMasterData { get; private set; }

        public DataTable DtParentLogTableData { get; private set; }

        public DataTable DtChildLogTableData { get; private set; }

        public string ParentTableName
        {
            get { return (string)GetValue(ParentTableNameProperty); }
            set { SetValue(ParentTableNameProperty, value); }
        }

        public string ChildTableName
        {
            get { return (string)GetValue(ChildTableNameProperty); }
            set { SetValue(ChildTableNameProperty, value); }
        }

        public string TempParentTable
        {
            get { return (string)GetValue(TempParentTableProperty); }
            set { SetValue(TempParentTableProperty, value); }
        }

        public string TempChildTable
        {
            get { return (string)GetValue(TempChildTableProperty); }
            set { SetValue(TempChildTableProperty, value); }
        }

        public string LogParentTable
        {
            get { return (string)GetValue(LogParentTableProperty); }
            set { SetValue(LogParentTableProperty, value); }
        }
        
        public string LogChildTable
        {
            get { return (string)GetValue(LogChildTableProperty); }
            set { SetValue(LogChildTableProperty, value); }
        }

        public string LogTableName
        {
            get { return (string)GetValue(LogTableNameProperty); }
            set { SetValue(LogTableNameProperty, value); }
        }

        public Modes FormMode
        {
            get { return (Modes)GetValue(FormModeProperty); }
            set
            {
                SetValue(FormModeProperty, value);
                EnableDisableButtons(FormMode);
            }
        }

        public string FirstFocusElement
        {
            get { return firstFocusElement; }
            set { firstFocusElement = value; }
        }

        public DataRow CurrentRow
        {
            get { return (DataRow)GetValue(CurrentRowProperty); }
            set { SetValue(CurrentRowProperty, value); }
        }

        public int CurrentIndex
        {
            get { return currentIndex; }
            set
            {
                currentIndex = value;
            }
        }

        public string UniqueKey
        {
            get { return uniqueKey; }
            set
            {
                uniqueKey = value;
            }
        }

        public string PrimaryKeyField
        {
            get { return primaryKeyField; }
            set
            {

                if (value == null)
                {
                    primaryKeyField = "PK";
                }
                else
                {
                    primaryKeyField = value;
                }
            }
        }
        
        public string ReferenceNoField
        {
            get { return referenceNoField; }
            set
            {
                if (value == null)
                {
                    referenceNoField = "RefNo";
                }
                else
                {
                    referenceNoField = value;
                }
            }
        }
        

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        public string ReportFolder
        {
            get { return (string)GetValue(ReportFolderProperty); }
            set { SetValue(ReportFolderProperty, value); }
        }

        public string ReportName
        {
            get { return reportName; }
            set { reportName = value; }
        }
        public bool IsValid
        {
            get { return (bool)GetValue(IsValidProperty); }
            set { SetValue(IsValidProperty, value); }
        }

        public SqlTransaction SqlTran
        {
            get { return (SqlTransaction)GetValue(SqlTranProperty); }
            set { SetValue(SqlTranProperty, value); }
        }

        public string IgnoreTableList
        {
            get { return (string)GetValue(IgnoreTableListProperty); }
            set { SetValue(IgnoreTableListProperty, value); }
        }

        public DataTable DtChildTxnData
        {
            get { return (DataTable)GetValue(DtChildTxnDataProperty); }
            set { SetValue(DtChildTxnDataProperty, value); }
        }

        public DataTable DtParentTxnData
        {
            get { return (DataTable)GetValue(DtParentTxnDataProperty); }
            set { SetValue(DtParentTxnDataProperty, value); }
        }

        #endregion

        #region Methods Execute Type

        public bool IsDataValidateExecute
        {
            get { return (bool)GetValue(IsDataValidateExecuteProperty); }
            set { SetValue(IsDataValidateExecuteProperty, value); }
        }

        public bool IsDataUpdValidateExecute
        {
            get { return (bool)GetValue(IsDataUpdValidateExecuteProperty); }
            set { SetValue(IsDataUpdValidateExecuteProperty, value); }
        }

        public bool IsDataDelValidateExecute
        {
            get { return (bool)GetValue(IsDataDelValidateExecuteProperty); }
            set { SetValue(IsDataDelValidateExecuteProperty, value); }
        }

        public bool IsDataSave
        {
            get { return (bool)GetValue(IsDataSaveProperty); }
            set { SetValue(IsDataSaveProperty, value); }
        }

        public bool IsDataUpdate
        {
            get { return (bool)GetValue(IsDataUpdateProperty); }
            set { SetValue(IsDataUpdateProperty, value); }
        }

        public bool IsDataDelete
        {
            get { return (bool)GetValue(IsDataDeleteProperty); }
            set { SetValue(IsDataDeleteProperty, value); }
        }

        public bool IsApproval
        {
            get { return (bool)GetValue(IsApprovalProperty); }
            set { SetValue(IsApprovalProperty, value); }
        }

        public bool IsResetDefaultExecute
        {
            get { return (bool)GetValue(IsResetDefaultExecuteProperty); }
            set { SetValue(IsResetDefaultExecuteProperty, value); }
        }

        #endregion

        #endregion

        #region Exposable Event Handlers

        public event EventHandler<EventArgs> PreDataValidation;             //use to data validate before internal method execution, when data saving
        public event EventHandler<EventArgs> PostDataValidation;            //use to data validate after internal method execution, when data saving
        public event EventHandler<EventArgs> PreDataDelValidation;          //use to data validate before internal method execution, when data delete record
        public event EventHandler<EventArgs> PostDataDelValidation;         //use to data validate after internal method execution, when data delete record
        public event EventHandler<EventArgs> PreDataInsert;                 //use to data saving processes before internal method execution, when data inserting
        public event EventHandler<EventArgs> PostDataInsert;                //use to data saving processes after internal method execution, when data inserting
        public event EventHandler<EventArgs> PreDataUpdate;                 //use to data saving processes before internal method execution, when data updating
        public event EventHandler<EventArgs> PostDataUpdate;                //use to data saving processes after internal method execution, when data updating
        public event EventHandler<EventArgs> PreDataDelete;                 //use to data deletion processes before internal method execution, when data deleting
        public event EventHandler<EventArgs> PostDataDelete;                //use to data deletion processes after internal method execution, when data deleting
        public event EventHandler<EventArgs> RefreshExternalDataObjects;    //use to external data population 
        public event EventHandler<EventArgs> PostDataSaveProcess;           //use to external data population 
        public event EventHandler<EventArgs> PreResetDefault;               //use to reset form data before internal method execution
        public event EventHandler<EventArgs> PostResetDefault;              //use to reset form data after internal method execution
        
        #region Exposable Events for Additionl Buttons

        public event EventHandler<EventArgs> ExecuteButton1Event;
        public event EventHandler<EventArgs> ExecuteButton2Event;
        public event EventHandler<EventArgs> ExecuteButton3Event;
        public event EventHandler<EventArgs> ExecuteButton4Event;
        public event EventHandler<EventArgs> ExecuteButton5Event;

        #endregion

        #endregion

        #region Property Changed Callback Methods

        private static bool TableNameValidateCallBack(object _tableName)
        {
            return !_tableName.Equals("");
        }

        private static void FormModeChangedCallBack(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            TransactionToolBar tansactionToolBar = depObj as TransactionToolBar;

            if (tansactionToolBar != null)
                tansactionToolBar.EnableDisableButtons((Modes)e.NewValue);
        }

        private static void OnCurrentRowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TransactionToolBar tansactionToolBar = d as TransactionToolBar;

            if (tansactionToolBar != null)
            {
                if (tansactionToolBar.RefreshExternalDataObjects != null)
                    tansactionToolBar.RefreshExternalDataObjects(tansactionToolBar, new EventArgs());
            }
        }

        #endregion 

        #region User Control Commands

        #region AddNew Button

        private void ExecuteAddNew(object sender, RoutedEventArgs e)
        {
            FormMode = Modes.New;
            IsValid = false;
            NewDataRow = DtParentTxnData.NewRow();
            CurrentRow = NewDataRow;
            SetFirstFocusElement();
        }       

        #endregion

        #region Edit Button

        private void ExecuteEdit(object sender, RoutedEventArgs e)
        {
            if (DtParentTxnData != null && DtParentTxnData.Rows.Count > 0)
            {
                FormMode = Modes.Edit;
                IsValid = false;
                OldDataRow = DtParentTxnData.NewRow();
                foreach (DataColumn item in CurrentRow.Table.Columns)
                {
                    OldDataRow[item] = CurrentRow[item];
                } 
                SetFirstFocusElement();
            }
            else
                MessageService.ShowMessage(MessageText.EmptyTableDeleteEditBlock, Title, MessageType.InfoOK);
        }

        #endregion

        #region Delete Button

        private void ExecuteDelete(object sender, RoutedEventArgs e)
        {
            
        }

        #endregion

        #region Undo Button

        private void ExecuteUndo(object sender, RoutedEventArgs e)
        {
            ResetDefaults();
        }

        #endregion

        #region Navigation Buttons

        #region Move into previous record
        private void ExecutePrev(object sender, RoutedEventArgs e)
        {
            
        }
        #endregion

        #region Move into next record
        private void ExecuteNext(object sender, RoutedEventArgs e)
        {

        }
        #endregion

        #endregion

        #region Save Button

        private void ExecuteSave(object sender, RoutedEventArgs e)
        {
            IsValid = true;
            if (DataValidation())
            {
                NewDataRow = CurrentRow;

                if (FormMode == Modes.New)
                    InsertMasterRecord();
                else if (FormMode == Modes.Edit)
                    UpdateMasterRecord();

                if (IsValid)
                {
                    MessageService.ShowMessage(MessageText.SavedSuccessfully, Title, MessageType.InfoOK);
                }
                else
                    MessageService.ShowMessage(MessageText.ErrorWhileSaving, Title, MessageType.InfoOK);

                ResetDefaults();
            }
        }
                
        #endregion

        #region Close Button

        private void ExecuteClose(object sender, RoutedEventArgs e)
        {
            Window parentwin = Window.GetWindow(this);
            if (parentwin != null)
                parentwin.Close();
        }

        #endregion

        #region Print Button

        private void ExecutePrint(object sender, RoutedEventArgs e)
        {
            
        }

        #endregion

        #region Approve Button
        private void ExecuteApprove(object sender, RoutedEventArgs e)
        {

        }

        #endregion

        #region View Button
        private void ExecuteView(object sender, RoutedEventArgs e)
        {
            FormMode = Modes.View;
        }

        #endregion

        #region Button1 Button

        private void ExecuteButton1(object sender, RoutedEventArgs e)
        {
            
        }

        #endregion

        #region Button2 Button

        private void ExecuteButton2(object sender, RoutedEventArgs e)
        {
            
        }

        #endregion

        #region Button3 Button

        private void ExecuteButton3(object sender, RoutedEventArgs e)
        {
           
        }

        #endregion

        #region Button4 Button

        private void ExecuteButton4(object sender, RoutedEventArgs e)
        {
            
        }

        #endregion

        #region Button5 Button

        private void ExecuteButton5(object sender, RoutedEventArgs e)
        {
            
        }

        #endregion

        #region Transaction Tool Bar Loaded

        private void TransactionToolBarLoad(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            IsValid = false;
            ResetDefaults();
            var window = Window.GetWindow(this);
            if (window != null)
            {
                window.Title = Title;
                window.KeyDown += HandleKeyPress;           //use to manage the enter key press in parent window
            }
        }  

        #endregion

        #region Parent Window Enter Key Press Handling

        private void HandleKeyPress(object sender, KeyEventArgs e)
        {
            var parentWindow = Window.GetWindow(this);
            if (parentWindow != null)
            {
                var Uie = (UIElement)Keyboard.FocusedElement;
                if (Uie != null)
                {
                    if (e.Key == Key.Enter)
                        Uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                }
            }
        }

        #endregion

        #endregion

        #region Private Methods

        private void UpdateMasterRecord()
        {
            throw new NotImplementedException();
        }

        private void InsertMasterRecord()
        {
            if (PreDataInsert != null)
                PreDataInsert(this, new EventArgs());

            if (IsValid && IsDataSave)
            {
                if (TransactionWriteService != null)
                {
                    var tran = SqlTran;
                    if (TransactionWriteService.InsertMasterData(ParentTableName,ChildTableName, PrimaryKeyField, NewDataRow, DtChildTxnData, ref  tran))
                    {
                        primaryKey = TransactionWriteService.PrimaryKey;
                        CurrentRow[PrimaryKeyField] = primaryKey;
                        SqlTran = tran;
                        if (PostDataInsert != null && IsValid)
                            PostDataInsert(this, new EventArgs());
                    }
                    else
                    {
                        IsValid = false;
                        SqlTran = tran;
                        if (SqlTran != null)
                            SqlTran.Rollback();
                    }
                }
            }

            if (IsValid)
            {
                if (PostDataSaveProcess != null)
                {
                    PostDataSaveProcess(this, new EventArgs());
                }
                if (SqlTran != null)
                    SqlTran.Commit();
            }
            else
            {
                if (SqlTran != null && SqlTran.Connection != null)
                    SqlTran.Rollback();
            }
        }
        
        private bool DataValidation()
        {
            if (PreDataValidation != null)
                PreDataValidation(this, new EventArgs());

            if (IsValid && IsDataValidateExecute)
            {
                
            }
            if (PostDataValidation != null && IsValid)
                PostDataValidation(this, new EventArgs());

            return IsValid;
        }

        private void SetFirstFocusElement()
        {
            var parentWindow = Window.GetWindow(this);

            ContentControl contetn = (ContentControl)parentWindow.FindName("popupContent");

            DependencyObject contentPresenter = VisualTreeHelper.GetChild(contetn, 0);

            UserControl userControl = VisualTreeHelper.GetChild(contentPresenter, 0) as UserControl;


            var uie = (UIElement)userControl.FindName(FirstFocusElement);

            if (FormMode == Modes.Edit)
                uie.Focus();
            else if (FormMode == Modes.New)
                uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }

        private void ResetDefaults()
        {
            if (PreResetDefault != null)
                PreResetDefault(this, new EventArgs());

            if (IsResetDefaultExecute)
            {
                LoadDataTables();
                LoadLogTables();
                FormMode = Modes.None;
                SetCurrentRecord(0);
                SqlTran = null;
                IsValid = true;
            }

            if (PostResetDefault != null)
                PostResetDefault(this, new EventArgs());

            
        }

        private void SetCurrentRecord(int rowIndex)
        {
            if (DtParentTxnData != null && DtParentTxnData.Rows.Count > 0)
            {
                currentIndex = DtParentTxnData.Rows.Count <= rowIndex ? rowIndex - 1 : rowIndex;
                CurrentRow = DtParentTxnData.Rows[currentIndex];
                CurrentIndex = currentIndex;
            }
            else
                CurrentRow = DtParentTxnData.NewRow();

            EnableDisableButtons();
        }

        private void LoadLogTables()
        {
            if (TransactionReadService != null)
            {
                if (IsApproval)
                {
                    DtParentLogTableData = TransactionReadService.GetDummyRecord(LogParentTable);
                    DtChildLogTableData = TransactionReadService.GetDummyRecord(LogChildTable);
                }
            }
        }

        private void LoadDataTables()
        {
            if (TransactionReadService != null)
            {
                if (IsApproval)
                {
                    DtParentTxnData = TransactionReadService.GetDummyRecord(TempParentTable);
                    DtChildTxnData= TransactionReadService.GetDummyRecord(TempChildTable);
                }
                else
                {
                    DtParentTxnData = TransactionReadService.GetDummyRecord(ParentTableName);
                    DtChildTxnData = TransactionReadService.GetDummyRecord(ChildTableName);
                }
            }
        }

        //Change properties when form mode changing
        private void EnableDisableButtons(Modes formMode)
        {
            isNavigationEnable = false;
            switch (formMode)
            {
                case Modes.New:
                    isSaveEnable = true;
                    break;
                case Modes.Edit:
                    isSaveEnable = true;
                    break;
                case Modes.Delete:
                    isSaveEnable = false;
                    break;
                case Modes.Approve:
                    isSaveEnable = true;
                    break;
                case Modes.Print:
                    isSaveEnable = false;
                    break;
                case Modes.Save:
                    break;
                case Modes.Undo:
                    break;
                case Modes.None:
                    isSaveEnable = false;
                    break;
                case Modes.View:
                    isNavigationEnable = true;
                    break;
                case Modes.Cancel:
                    break;
                default:
                    break;
            }
            EnableDisableButtons();
        }

        //Enable Dissable buttons due to changes of FormMode & CurrentIndex
        private void EnableDisableButtons()
        {
            btnNew.IsEnabled = (!isSaveEnable);
            btnEdit.IsEnabled = (!isSaveEnable);
            btnDelete.IsEnabled = (!isSaveEnable);
            btnUndo.IsEnabled = (isSaveEnable);
            //btnFirst.IsEnabled = (!isSaveEnable && (CurrentIndex != 0) && (DtAllMasterData.Rows.Count > 0));
            btnPrevious.IsEnabled = (isNavigationEnable);
            btnNext.IsEnabled = (isNavigationEnable);
            //btnLast.IsEnabled = (!isSaveEnable && (CurrentIndex != DtAllMasterData.Rows.Count - 1) && (DtAllMasterData.Rows.Count > 0));
            btnPrint.IsEnabled = (!isSaveEnable);
            btnSave.IsEnabled = (isSaveEnable);
            btnClose.IsEnabled = (!isSaveEnable);
        }
        #endregion
    }

}